/************************************************************
 * Project Name         [Thinking_In_Java]
 * File Name            [Java_IO_ByteArrayStream.java]
 * Creation Date        [07-Jul-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_09_io.api;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 * java.io.ByteArrayInputStream<br>
 * java.io.ByteArrayOutputStream<br>
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class Java_IO_ByteArrayStream {

	/**
	 * 测试方法
	 * 
	 * @param args
	 *            命令行参数
	 */
	public static void main(String[] args) {
		// 需要DataOutputStream包装
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(baos);

		try {
			dos.writeBoolean(true);
			dos.writeChar('c');
			dos.writeByte(5);
			dos.writeShort(10);
			dos.write(20);
			dos.writeLong(100L);
			dos.writeFloat(30.0F);
			dos.writeDouble(40.0);
			dos.writeUTF("test write");
		} catch (IOException e) {
			e.printStackTrace();
		}

		// 需要DataInputStream包装
		ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
		DataInputStream dis = new DataInputStream(bais);

		try {
			System.out.println(dis.readBoolean());
			System.out.println(dis.readChar());
			System.out.println(dis.readByte());
			System.out.println(dis.readShort());
			System.out.println(dis.read());
			System.out.println(dis.readLong());
			System.out.println(dis.readFloat());
			System.out.println(dis.readDouble());
			System.out.println(dis.readUTF());
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			dos.close();
			dis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
